<template>
    <rui-page :defines="defines">
        <rui-table :defineId="'batchInfo'"
                   :fixedLeft="['systemName']"
                   :fixedRight="['action']"
                   :showIndex="false"
                   :columns="['systemName','batchDate','systemStatus','liveThreadNum','errorNoticeType','action']"
                   :select="false"
                   :slots="[{key:'action',slot:'action'}]"
                   @loadDatas="queryBatchInfo"
                   :page-able="false"
                   ref="batchInfoTable">
            <template slot="toolBar">
                <Button @click="addBatchInfo"  v-has="'afs-batch-config'" icon="ios-add" type="primary" >新增</Button>
                <Button @click="refreshBatchInfo" icon="md-refresh-circle" type="info" >刷新</Button>
            </template>
            <template slot="action" slot-scope="{ row }">
                <div>
                    <Button @click="editBatchInfo(row)"  v-has="'afs-batch-config'" size="small" type="primary">编辑</Button>
                    <Button @click="removeBatchInfo(row)"  v-has="'afs-batch-config'" size="small" type="primary">删除</Button>
                    <Button v-if="row.systemStatus=='N'" @click="startBatch(row)" size="small" type="primary">启动批量</Button>
                    <Button v-else-if="row.systemStatus=='E'&&row.liveThreadNum==0" @click="changeToNormal(row)" size="small" type="error">重置状态</Button>
                </div>
            </template>
        </rui-table>
        <Modal
            :closable="false"
            :mask-closable="false"
            :title="batchInfo.configTitle"
            :width="800"
            v-model="batchInfo.show">
            <rui-form :form-options="batchInfo.options"
                      :label-width="120"
                      :read-only="false"
                      ref="batchInfoForm">
            </rui-form>
            <Form :label-width="100" v-if="errorNoticeType=='dingtalk'" :rules="dingTalkConfigValidate" :model="dingTalkConfig"
                  ref="dingTalkConfigForm">
                <FormItem label="机器人uri" prop="hockUri">
                    <Input placeholder="机器人uri" type="text" v-model="dingTalkConfig.hockUri">
                    </Input>
                </FormItem>
                <FormItem label="密钥" prop="url">
                    <Input placeholder="密钥" type="text" v-model="dingTalkConfig.security">
                    </Input>
                </FormItem>
            </Form>
            <div slot="footer">
                <Button @click="batchInfo.show = false" size="small">取消</Button>
                <Button @click="saveBatchInfo" size="small" type="primary">保存</Button>
            </div>
        </Modal>
        <Modal
            :closable="false"
            :mask-closable="false"
            :title="startBatchInfo.title"
            :width="400"
            v-model="startBatchInfo.show">
            <Select v-model="startBatchInfo.startNo" style="width:370px">
                <Option v-for="job in startBatchInfo.jobList" :value="job.batchNo" :key="job.id">{{ job.batchNo+"-"+job.batchName }}</Option>
            </Select>
            <div slot="footer">
                <Button @click="startBatchInfo.show = false" size="small">取消</Button>
                <Button @click="startRemoteBatch" size="small" type="primary">启动</Button>
            </div>
        </Modal>
    </rui-page>
</template>

<script>
import {listAllBatchInfo,editBatchInfo,removeBatchInfo,listJobGroup,batchStart,resetError} from '_p/basic/api/batch/batchInfo'
import batchInfoConfig from './defines/batch-info-define'
import {deepClone} from "@/libs/utils/ObjectClone";
export default {
    name: "batch-config",
    data() {
        return {
            defines:[{
                id:'batchInfo',
                fields:batchInfoConfig
            }],
            batchInfo:{
                configTitle:'新增批量',
                configType:'add',
                show:false,
                options: [
                    {
                        isGroup: false,
                        grids: [
                            {defineId: 'batchInfo', span: 12, fields: ['systemName']},
                            {defineId: 'batchInfo', span: 12, fields: ['batchDate']},
                        ]
                    },{
                        isGroup: false,
                        grids: [
                            {defineId: 'batchInfo', span: 12, fields: ['errorNoticeType']}
                        ]
                    }
                ]
            },
            errorNoticeType:'dingtalk',
            dingTalkConfig:{
                security:'',
                hockUri:''
            },
            dingTalkConfigValidate: {
                hockUri: [
                    { type:'url', message: '机器人地址不正确', trigger: 'blur' }
                ]
            },
            startBatchInfo:{
                show:false,
                title:'启动批量',
                startNo:'',
                systemName:'',
                jobList:[]
            }
        };
    },
    methods: {
        queryBatchInfo(queryData) {
            listAllBatchInfo().then(res => {
                if (res.code === "0000") {
                    this.$refs.batchInfoTable.updateTableData(res.data, 100);
                }
            });
        },
        refreshBatchInfo(){
            this.$refs['batchInfoTable'].reloadData();
        },
        addBatchInfo(){
            this.batchInfo.configTitle = '新建批量信息';
            this.$refs.batchInfoForm.setAllFieldRequired(true);
            this.batchInfo.configType = 'add';
            let self = this;
            this.dingTalkConfig.hockUri = '';
            this.dingTalkConfig.security = '';
            this.$nextTick(() => {
                self.$refs.batchInfoForm.resetFrom();
                self.batchInfo.show = true;
            })
        },
        editBatchInfo(row){
            this.batchInfo.configTitle = '维护'+row.systemName+'批量信息';
            this.batchInfo.configType = 'modify';
            let self = this;
            self.$refs.batchInfoForm.resetFrom();
            this.$refs.batchInfoForm.updateFormData(deepClone(row));
            let errorConfig = JSON.parse(row.errorNoticeConfig);
            if(errorConfig.hockUri){
                this.dingTalkConfig.hockUri = errorConfig.hockUri;
            }else{
                this.dingTalkConfig.hockUri = '';
            }
            if(errorConfig.security){
                this.dingTalkConfig.security = errorConfig.security;
            }else{
                this.dingTalkConfig.security = '';
            }
            this.$nextTick(() => {
                self.batchInfo.show = true;
                this.$refs.batchInfoForm.setAllFieldRequired(true);
            })
        },
        removeBatchInfo(row){
            let self = this;
            this.$Modal.confirm({
                title: "确认",
                content: `确认删除批量[${row.systemName}]？`,
                onOk: () => {
                    removeBatchInfo(row.systemName).then(res => {
                        self.refreshBatchInfo();
                    })
                }
            });
        },
        saveBatchInfo(){
            let self = this;
            this.$refs.batchInfoForm.getForm().validate((valid) => {
                if (valid) {
                    let reqData = self.$refs.batchInfoForm.getFormData();
                    if (self.batchInfo.configType === 'add') {
                        reqData.systemStatus='N';
                        reqData.liveThreadNum=0;
                    }
                    self.$refs.dingTalkConfigForm.validate(val=>{
                        if(val) {
                            reqData.errorNoticeConfig = JSON.stringify(self.dingTalkConfig);
                            editBatchInfo(reqData).then(res => {
                                if (res.code === '0000') {
                                    self.refreshBatchInfo();
                                    self.batchInfo.show = false;
                                } else {
                                    self.$Message.error(res.msg);
                                }
                            }).catch(err => {

                            })
                        }
                    })
                } else {
                    self.batchInfo.show = true;
                    self.$Message.error('数据不完整!');
                }
            })
        },
        startBatch(row){
            this.startBatchInfo.jobList.splice(0,this.startBatchInfo.jobList.length);
            this.startBatchInfo.startNo = '';
            this.startBatchInfo.systemName = row.systemName;
            let self = this;
            listJobGroup(row.systemName).then(res => {
                if (res.code === '0000') {
                    this.startBatchInfo.jobList.push(...res.data)
                    self.startBatchInfo.show = true;
                } else {
                    self.$Message.error(res.msg);
                }
            }).catch(err => {

            })
        },
        changeToNormal(row){
            let self = this;
            resetError(row.systemName).then(res => {
                if (res.code === '0000') {
                    self.refreshBatchInfo();
                } else {
                    this.$Message.error(res.msg);
                }
            }).catch(err => {

            })
        },
        startRemoteBatch(){
            let self = this;
            if(!this.startBatchInfo.startNo||!this.startBatchInfo.systemName ||this.startBatchInfo.startNo === ''||this.startBatchInfo.systemName === ''){
                this.$Message.error('请选择启动批量步骤');
            }else{
                batchStart(this.startBatchInfo.systemName,this.startBatchInfo.startNo).then(res => {
                    if (res.code === '0000') {
                        this.$Message.success("批量启动成功");
                        this.startBatchInfo.jobList.splice(0,this.startBatchInfo.jobList.length);
                        this.startBatchInfo.startNo = '';
                        this.startBatchInfo.systemName = '';
                        this.startBatchInfo.show = false;
                        self.$emit('changeTab','index-monitor')
                        self.refreshBatchInfo();
                    } else {
                        this.$Message.error(res.msg);
                    }
                }).catch(err => {

                })
            }
        }
    },
    computed: {

    },
    created(){

    },
    mounted() {
        this.$refs['batchInfoTable'].reloadData();
    }
};
</script>
